<section>
	<div class="pending-detial">
		<h2><i class="fa fa-shopping-cart kcart"></i>购物车</h2>
	

		{if:$this->count == 0}
		<div class="tips_msg">
			购物车内暂时没有商品，赶紧去选购吧！
		</div>

		<div class="container">
			<input type='button' class="btn  btn-block" value='去逛逛' onclick='window.location.href="{url:}";' />
		</div>

		{else:}

	
		<div class="alert alert-danger" id="cart_prompt" {if:empty($this->promotion)}style="display:none"{/if}><i class="fa fa-exclamation-circle"></i> 恭喜，您的订单已经满足了以下优惠活动！{foreach:items = $this->promotion}
			{$item['plan']}，{$item['info']}
				{/foreach} 
		  </div>

		
<!--商品列表 开始-->

{foreach:items = $this->goodsList}
{set:$type=isset($item['spec_array'])?'product':'goods'}
{set:$item['id']=isset($item['spec_array'])?$item['product_id']:$item['goods_id']}	
	<div class="row " style="padding-top:10px;padding-bottom:10px;border-bottom:1px solid #ccc">
		<div class="col-md-12 column" >
			<div class="left">
			<a href="{url:/site/products/id/$item[goods_id]}">
			<img src="{url:/pic/thumb/img/$item[img]/w/80/h/80}" width="80px" height="80px" alt="{$item['name']}" title="{$item['name']}" />
			</a>

			</div>
			<div class="left" style="padding-left:10px;">
			<h7 class="khidden">
			{echo: IString::substr($item['name'], $length = 50);} 
			</h7>

			<p style="margin-bottom:0;line-height:16px">
			<small>单 价：￥{$item['sell_price']}</small>
			</p>
			{if:isset($item['spec_array'])}			
			{set:$spec_array=Block::show_spec($item['spec_array']);}
			{foreach:items=$spec_array item=$specValue key=$specName}
			<p style="margin-bottom:0;line-height:16px">
			<small>
			
			{$specName}：{$specValue} &nbsp&nbsp
			</small>
			</p>
			{/foreach}
			
			{/if}

			<p style="margin-bottom:0;line-height:16px">
			<small>小 计：￥<strong id="{$type}_sum_{$item['id']}">{$item['sum']}</strong></small>
			</p>

			</div>
		</div>



	<div class="col-md-12 col-xs-12 column" >
			{set:$item_json = JSON::encode($item)}
			<div style='float:right'>
			<span  onclick='cart_reduce("{$type}",{$item_json});' class="edit" type="button" >-</span>
			<input  style='width:27px;height:22px;' class="edit edit1" type="text" id="{$type}_count_{$item['id']}" value="{$item['count']}" onblur='cartCount("{$type}",{$item_json});'>
			<span  onclick='cart_increase("{$type}",{$item_json});' class="edit" type="button" >+</span>	

			<i class="fa fa-trash-o kcart" style="margin-left:10px" onclick='window.location.href="{url:/simple/removeCart/link/cart/type/$type/goods_id/$item[id]}";'></i>		

		

			</div>
	</div>

		</div>

	</div>

{/foreach}

<!--商品列表 结束-->

	<div class="row" style="margin-top:10px">
		<div class="col-md-12">
			<div class="total-table">
				<table class="table table-bordered">
					<tbody>
					<tr>
					<td class="text-right"><strong>商品总重量:</strong></td>
					<td class="text-right" id='weight'>{$this->weight}g</td>
					</tr>
					<tr>
					<td class="text-right"><strong>商品总金额:</strong></td>
					<td class="text-right" id='origin_price'>{$this->sum} </td>
					</tr>
					<tr>
					<td class="text-right"><strong>商品优惠:</strong></td>
					<td class="text-right" id='discount_price'>{$this->reduce}</td>
					</tr>
					<tr>
					<td class="text-right"><strong>促销活动优惠:</strong></td>
					<td class="text-right" id='promotion_price'>{$this->proReduce}</td>
					</tr>

					<tr>
					<td class="text-right"><strong>金额总计（不含运费）:</strong></td>
					<td class="text-right" id='sum_price'>{$this->final_sum}</td>
					</tr>

					</tbody>
				</table>
			</div>
		</div>
	</div>

	<div class="row clearfix">
		<div class="col-md-6  col-xs-6 column" style="margin-bottom:10px">
			<button type="button" class="btn  btn-block"  onclick="window.location.href='{url:$callback}';">继续购物</button>
		</div>
		{if:$this->goodsList}
		<div class="col-md-6 col-xs-6 column" style="margin-bottom:10px">
			<button type="button" id="btn_pay" class="btn  btn-block" onclick="check_finish();" >去结算</button>
		</div>
		{/if}
	</div>

	 
	    {/if}
	</div>
</section>

<script type='text/javascript'>
//购物车数量改动计算
function cartCount(type,obj,oldCount)
{
	var countInput    = $('#'+type+'_count_'+obj.id);
	var countInputVal = parseInt(countInput.val());

	//商品数量大于1件
	if(isNaN(countInputVal) || (countInputVal <= 0))
	{
		alert('购买的数量必须大于1件');
		countInput.val(1);
		cartCount(type,obj,oldCount);
	}

	//商品数量小于库存量
	else if(countInputVal > parseInt(obj.store_nums))
	{
		alert('购买的数量不能大于此商品的库存量');
		countInput.val(parseInt(obj.store_nums));
		cartCount(type,obj,oldCount);
	}
	else
	{
		//修改按钮状态
		countInput.attr('disabled',true);
		//$('#btn_pay').val('wait');

		//获取之前的购买数量
		if(oldCount == undefined)
		{
			oldCount = countInput.data('oldCount') ? parseInt(countInput.data('oldCount')) : parseInt(obj.count);
		}

		//修改的购买数量
		var changeNum = parseInt(countInput.val()) - oldCount;

		//商品数量没有改动
		if(changeNum == 0)
		{
			//修改按钮状态
			countInput.attr('disabled',false);
			//$('#btn_pay').val('ok');
			return '';
		}

		//更新购物车中此商品的数量
		$.getJSON("{url:/simple/joinCart}",{"type":type,"goods_id":obj.id,"goods_num":changeNum,"random":Math.random()},function(content){
			if(content.isError == true)
			{
				alert(content.message);
				var countInput = $('#'+type+'_count_'+obj.id);

				//上次数量回填
				if(changeNum < 0)
				{
					countInput.val(parseInt(countInput.val() - changeNum));
				}
				else
				{
					countInput.val(parseInt(countInput.val() + changeNum));
				}

				//修改按钮状态
				countInput.attr('disabled',false);
				//$('#btn_pay').val('ok');
			}
			else
			{
				var countInput = $('#'+type+'_count_'+obj.id);

				//缓存旧的购买数量
				countInput.data('oldCount',parseInt(countInput.val()));

				/*变动的价格可能为负数(减少购买量)*/
				var smallSumC   = (mathSub(parseFloat(obj.sell_price),parseFloat(obj.reduce))) * changeNum; //价格变动
				var weightC     = mathMul(parseFloat(obj.weight),changeNum);       //重量变动
				var originC     = mathMul(parseFloat(obj.sell_price),changeNum);   //原始价格变动
				var discountC   = mathMul(parseFloat(obj.reduce),changeNum);       //优惠变动

				/*开始更新数据(1)*/

				//商品小结计算
				var smallSum    = $('#'+type+'_sum_'+obj.id);
				smallSum.html(mathAdd(parseFloat(smallSum.text()),smallSumC));

				//最终重量
				$('#weight').html(mathAdd(parseFloat($('#weight').text()),weightC));

				//原始金额
				$('#origin_price').html(mathAdd(parseFloat($('#origin_price').text()),originC));

				//优惠价
				$('#discount_price').html(mathAdd(parseFloat($('#discount_price').text()),discountC));

				//促销规则检测
				var final_sum   = mathSub(parseFloat($('#origin_price').text()),parseFloat($('#discount_price').text()));
				var tmpUrl = '{url:/simple/promotionRuleAjax/random/@random@}';
				tmpUrl = tmpUrl.replace("@random@",Math.random());
				$.getJSON( tmpUrl ,{final_sum:final_sum},function(content){
					if(content.promotion.length > 0)
					{
						$('#cart_prompt .indent').remove();

						for(var i = 0;i < content.promotion.length; i++)
						{
							$('#cart_prompt').append('<li class="indent">'+content.promotion[i].plan+'，'+content.promotion[i].info+'</li>');
						}
						$('#cart_prompt').show();
					}
					else
					{
						$('#cart_prompt .indent').remove();
						$('#cart_prompt').hide();
					}
					/*开始更新数据 (2)*/

					//促销活动
					$('#promotion_price').html(content.proReduce);

					//最终金额
					$('#sum_price').html(mathSub(mathSub(parseFloat($('#origin_price').text()),parseFloat($('#discount_price').text())),parseFloat($('#promotion_price').text())));

					//修改按钮状态
					countInput.attr('disabled',false);
					//$('#btn_pay').val('ok');
				});
			}
		});
	}
}

//增加商品数量
function cart_increase(type,obj)
{
	//库存超量检查
	var countInput = $('#'+type+'_count_'+obj.id);
	var oldCount   = parseInt(countInput.val());
	if(parseInt(countInput.val()) + 1 > parseInt(obj.store_nums))
	{
		alert('购买的数量大于此商品的库存量');
	}
	else
	{
		if(countInput.attr('disabled') == true)
		{
			return false;
		}
		else
		{
			countInput.attr('disabled',true);
		}
		countInput.val(parseInt(countInput.val()) + 1);
		cartCount(type,obj,oldCount);
	}
}

//减少商品数量
function cart_reduce(type,obj)
{
	//库存超量检查
	var countInput = $('#'+type+'_count_'+obj.id);
	var oldCount   = parseInt(countInput.val());
	if(parseInt(countInput.val()) - 1 <= 0)
	{
		alert('购买的数量必须大于1件');
	}
	else
	{
		if(countInput.attr('disabled') == true)
		{
			return false;
		}
		else
		{
			countInput.attr('disabled',true);
		}
		countInput.val(parseInt(countInput.val()) - 1);
		cartCount(type,obj,oldCount);
	}
}

//检测购买完成量
function check_finish()
{
	if($.trim($('#btn_pay').val()) == 'wait')
	{
		window.setInterval("check_finish()", 400);
	}
	else
	{
		window.location.href = '{url:/simple/cart2}';
	}
}

$(function()
{
	$("#kros_b").css({ color: "#ff0011" });
})
</script>